package com.zut.blocks.service.user.impl;

import com.zut.blocks.base.common.Login;
import com.zut.blocks.base.domain.User;
import com.zut.blocks.base.service.impl.GenericServiceImpl;
import com.zut.blocks.dao.user.UserRepository;
import com.zut.blocks.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.zut.blocks.base.util.CheckUtil.fail;
import static com.zut.blocks.base.util.CheckUtil.notEmpty;

/**
 * @author lxd
 * @date 2020/11/25
 */
@Service
public class UserServiceImpl extends GenericServiceImpl<User, Integer> implements UserService {

    @Resource
    UserRepository userRepository;
    @Override
    public List<User> getAllUser() {

        return userRepository.findAll();
//        return Collections.singletonList(new User());
    }

    @Override
    public User getUser(int id) {
        return userRepository.getOne(id);
    }

    @Override
    public User getUserByUserNumber(String userNumber) {
        return userRepository.findByUserNumber(userNumber);
    }

    @Override
    public Integer deleteUser(int id) {
        userRepository.deleteById(id);
        return null;
    }

    @Override
    public User saveUser(User user) {
        userRepository.save(user);
        return user;
    }

    @Override
    public List<User> saveUserList(List<User> list) {
        userRepository.saveAll(list);
        return null;
    }

    @Override
    public User updateUSer(User user) {
        return  userRepository.save(user);
    }

    @Override
    public Login login(String username, String password) {
        notEmpty(username, "用户名不能为空");
        notEmpty(password, "密码不能为空");
        User user = userRepository.findByUserNumber(username);
        if (user == null) {
            fail("用户名或密码错误");
        }
        assert user != null;
        if (!user.getPassword().equals(password)) {
            fail("用户名或密码错误");
        }
        Login login = new Login();
        login.setId(user.getId());
        login.setName(user.getName());
        login.setType(user.getUserTypes());

        return login;
    }
}
